const { app, BrowserWindow, ipcMain, Tray, nativeImage, Menu } = require('electron')
const path = require('path')
const electron = require('electron')

// 在全局范围声明 mainWindow
let mainWindow = null

function createWindow() {

  mainWindow = new BrowserWindow({
    width: 1376,
    height: 800,
    minWidth: 1000,
    minHeight: 750,
    frame: false,
    autoHideMenuBar: true,
    transparent: true,   // 使背景透明
    animationEnabled: true,  // 启用动画
    visualEffectState: 'active',  // 视觉效果状态
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      enableRemoteModule: true,
      devTools: true,
      preload: path.resolve(__dirname, './preload.js')
    },
  })

  let env = 'pro1'
  if (env == 'pro') {
    ipcMain.on('move-window', (event, deltaX, deltaY) => {
      const bounds = mainWindow.getBounds()
      mainWindow.setBounds({
        x: bounds.x + deltaX,
        y: bounds.y + deltaY,
        width: bounds.width,
        height: bounds.height
      })
    })
    ipcMain.on('close-window', () => {
      mainWindow.hide()
      mainWindow.setSkipTaskbar(true)
      new electron.Notification({
        title: '音韵星空 - 提示',
        body: '应用已最小化到系统托盘',
        icon: path.join(__dirname, 'public/logo.ico')
      }).show()
    })
    ipcMain.on('mini-window', () => {
      mainWindow.minimize()
    })
    ipcMain.on('toggle-maximize', () => {
      if (mainWindow.isMaximized()) {
        mainWindow.unmaximize()
      } else {
        mainWindow.maximize()
      }
    })
    ipcMain.on('quit-app', () => {
      app.quit() // 直接退出应用
    })
    mainWindow.loadURL('http://localhost:5173')
  } else {
    // 窗口拖动
    ipcMain.on('move-window', (event, deltaX, deltaY) => {
      const bounds = mainWindow.getBounds()
      mainWindow.setBounds({
        x: bounds.x + deltaX,
        y: bounds.y + deltaY,
        width: bounds.width,
        height: bounds.height
      })
    })
    // 最小化
    ipcMain.on('mini-window', () => {
      mainWindow.minimize()
    })
    // 最大化或者还原
    ipcMain.on('toggle-maximize', () => {
      if (mainWindow.isMaximized()) {
        mainWindow.unmaximize()
      } else {
        mainWindow.maximize()
      }
    });
    // 关闭程序
    ipcMain.on('close-window', () => {
      mainWindow.hide()
      mainWindow.setSkipTaskbar(true)
      new electron.Notification({
        title: '音韵星空 - 提示',
        body: '应用已最小化到系统托盘',
        icon: path.join(__dirname, 'public/logo.ico')
      }).show()
    })
    ipcMain.on('quit-app', () => {
      app.quit() // 直接退出应用
    })
    mainWindow.loadFile(path.join(__dirname, 'dist/index.html'));
  }

  // 添加恢复窗口事件处理
  ipcMain.on('restore-window', () => {
    mainWindow.show()
    mainWindow.setSkipTaskbar(false)
  })

  // 创建托盘图标
  const icon = nativeImage.createFromPath(path.join(__dirname, 'public/logo.ico')) // 替换成你的图标路径
  let tray = new Tray(icon)
  
  tray.setToolTip('音韵星空')
  tray.on('click', () => {
    if (!mainWindow.isVisible()) {
      mainWindow.show()
    }
    if (mainWindow.isMinimized()) {
      mainWindow.restore()
    }
    mainWindow.setSkipTaskbar(false)
    mainWindow.focus()
  })

  // 修改右键菜单
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '显示主界面',
      click: () => {
        if (!mainWindow.isVisible()) {
          mainWindow.show()
        }
        if (mainWindow.isMinimized()) {
          mainWindow.restore()
        }
        mainWindow.setSkipTaskbar(false)
        mainWindow.focus()
      }
    },
    {
      label: '退出',
      click: () => {
        app.quit()
      }
    }
  ])
  tray.setContextMenu(contextMenu)
}

app.whenReady().then(() => {
  createWindow()
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('before-quit', () => {
  if (mainWindow && !mainWindow.isDestroyed()) {
    mainWindow.removeAllListeners('close')
    mainWindow.close()
  }
})
